<?php

/**
 * @author LonelyPersonal 
 * @copyright 26-8-2011;
 **/

class ph_Router {
     /*
     * @the registry
     */
     private $registry;
    
     /**
      *  @the controller path
     **/
     private $path;
    
     private $args = array();
    
     public $file;
    
     public $controller;
    
     public $action; 
    /**
     * Ham tao (constructor)
     * @param $registry
     * mot bien $registry se duoc tao khi ham duoc goi
     * @return void
     * *    
     function __construct($registry) {
       
            $this->registry = $registry;
     }
    
     /**
     *
     * @set controller directory path
     *
     * @param string $path
     *
     * @return void
     *
     */
     function setPath($path) {
    
    	/*** check if path is a directory ***/
    	if (is_dir($path) == false)
    	{
    		throw new Exception ('Duong dan khong dung : ' . $path . '`');
            return false; //twk thêm vô;
    	}
    	/*** set the path ***/
     	$this->path = $path;
    }
    
    
     /**
     *
     * @load the controller
     *
     * @access public
     *
     * @return void
     *
     */
     public function loader()
     {
    	/*** check the route ***/
    	$this->getController();
        
    	/*** if the file is not there diaf ***/
    	if (is_readable($this->file) == false) //If duong dan là không d?c du?c
    	{
    		$this->file = $this->path.'/error404Controller.php';
            $this->controller = 'error404';
    	}     
    	/*** include the controller ***/
    	include ($this->file);
    	
     }
    
    
     /**
     *
     * @get the controller
     *
     * @access private
     *
     * @return void
     *
     */
    private function getController() {
    
    	/*** get the route from the url ***/
    	$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];    
    	if (empty($route))
    	{
    		$route = 'index';
    	}
    	else
    	{
    		/*** get the parts of the route ***/
    		$parts = explode('/', $route); // C?t l?y t?ng ph?n c?a chu?i $route cho vào m?ng;
            
    		$this->controller = $parts[0]; // Set Controller (phàn sau d?u = ) c?a rt (L?y tên file s? du?c x? lý b?i Controller (bo di tien to c))
    		if(isset( $parts[1]))
    		{
    			$this->action = $parts[1];
    		}
    	}
    
    	if (empty($this->controller))
    	{
    		$this->controller = 'index';
    	}
    
    	/*** Get action ***/
    	if (empty($this->action))
    	{
    		$this->action = 'index';
    	}
    
    	/*** set the file path ***/
    	$this->file = $this->path .'/'. $this->controller . 'Controller.php'; 
    } 
}

?>